<template>
	<view class="container">
		<!-- 通讯录列表 -->
		<view class="list-container">
			<view 
				class="list-item" 
				v-for="(item, i) in contactList" 
				:key="item._id || i"
				@longpress="handleLongPress(item._id)"
			>
				<view class="item-content">
					<view class="item-row">
						<text class="label">序号:</text>
						<text class="value">{{i + 1}}</text>
					</view>
					<view class="item-row">
						<text class="label">用户名:</text>
						<text class="value">{{item.name || item.username || '未填写'}}</text>
					</view>
					<view class="item-row">
						<text class="label">手机号:</text>
						<text class="value">{{item.mobile || '未填写'}}</text>
					</view>
					<view class="item-row">
						<text class="label">Email:</text>
						<text class="value">{{item.email || '无'}}</text>
					</view>
				</view>
			</view>
			<view class="empty-tip" v-if="contactList.length === 0">
				<text>暂无通讯录数据</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				contactList: []
			};
		},
		onLoad() {
			this.getContactList();
		},
		onShow() {
			// 监听编辑页的刷新通知
			uni.$on('refreshContactList', () => {
				this.getContactList();
			});
		},
		onUnload() {
			// 移除监听
			uni.$off('refreshContactList');
		},
		methods: {
			async getContactList() {
				uni.showLoading({ title: '加载中...' });
				try {
					const res = await uniCloud.callFunction({
						name: 'getUser', 
						data: {}
					});
					if (res.result?.code === 200) {
						this.contactList = res.result.data || [];
					} else {
						this.contactList = res.result.data || [];
					}
				} catch (error) {
					uni.showToast({ title: '网络异常', icon: 'none' });
				} finally {
					uni.hideLoading();
				}
			},

			handleLongPress(id) {
				if (!id) {
					uni.showToast({ title: '该联系人无唯一标识', icon: 'none' });
					return;
				}
				uni.showActionSheet({
					itemList: ['删除联系人', '编辑联系人'],
					success: (res) => {
						if (res.tapIndex === 0) {
							this.deleteContact(id);
						} else if (res.tapIndex === 1) {
							// 关键修改：跳转路径改为 /pages/edit/edit
							uni.navigateTo({ url: `/pages/edit/edit?id=${id}` });
						}
					}
				});
			},

			async deleteContact(id) {
				const confirmRes = await uni.showModal({
					title: '确认删除',
					content: '是否要删除该联系人？',
					confirmText: '删除',
					cancelText: '取消'
				});

				if (confirmRes.confirm) {
					try {
						const res = await uniCloud.callFunction({
							name: 'removeUser', 
							data: { id }
						});

						if (res.result.code === 200) {
							uni.showToast({ title: '删除成功', icon: 'success' });
							this.getContactList();
						} else {
							uni.showToast({ title: res.result.msg || '删除失败', icon: 'none' });
						}
					} catch (error) {
						uni.showToast({ title: '删除失败，请稍后重试', icon: 'none' });
					}
				}
			}
		}
	};
</script>

<style scoped>
	.container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-top: 20rpx;
	}
	.list-container {
		padding: 0 20rpx;
	}
	.list-item {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	.item-content {
		line-height: 48rpx;
	}
	.item-row {
		display: flex;
	}
	.item-row .label {
		font-size: 28rpx;
		color: #666;
		width: 120rpx;
	}
	.item-row .value {
		font-size: 28rpx;
		color: #333;
	}
	.empty-tip {
		text-align: center;
		padding: 100rpx 0;
		color: #999;
		font-size: 28rpx;
	}
</style>